package controller.business.companyAssess;


import com.itextpdf.text.*;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import itext.ItextFont;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;

public class CompanyAssessPdf {
    public static void createPdf(OutputStream outputStream, Map<String,String> map) throws DocumentException, IOException {

        //设置纸张大小
        Document document = new Document(PageSize.A4,90.14F,90.14F,72F,72F);
        //建立一个书写器，与document对象关联
        PdfWriter.getInstance(document, outputStream);
        document.open();
        Paragraph title = new Paragraph("上海市质量技术监督局业务受理中心\n审查员/评审员工作纪律情况反馈表", ItextFont.fangZhengPdf18);
        title.setAlignment(Element.ALIGN_CENTER);
        document.add(title);

        //设置Table表格,创建一个7列的表格
        PdfPTable table = new PdfPTable(4);
        float width[] = {1.5F, 2, 1,2};//设置每列宽度比例
        table.setWidths(width);
        table.setWidthPercentage(100);//占页面宽度比例

        PdfPCell[] cell = new PdfPCell[14];
        cell[0] = new PdfPCell(new Phrase("申报单位名称", ItextFont.fangSongNormalPdf12));
        cell[1] = new PdfPCell(new Phrase(map.get("companyName"), ItextFont.fangSongNormalPdf12));
        cell[1].setColspan(3);
        cell[2] = new PdfPCell(new Phrase("评审组组长", ItextFont.fangSongNormalPdf12));
        cell[3] = new PdfPCell(new Phrase( map.get("leader"), ItextFont.fangSongNormalPdf12));

        cell[4] = new PdfPCell(new Phrase("评审日期", ItextFont.fangSongNormalPdf12));
        cell[5] = new PdfPCell(new Phrase(map.get("checkTime"), ItextFont.fangSongNormalPdf12));

        cell[6] = new PdfPCell(new Phrase("评审组组员", ItextFont.fangSongNormalPdf12));
        cell[7] = new PdfPCell(new Phrase( map.get("crew"), ItextFont.fangSongNormalPdf12));
        cell[7].setColspan(3);

        cell[8] = new PdfPCell(new Phrase("评审人员是否有收受礼品或现金情况，如有，请注明名称、数量、价值：\n"+map.get("bribery") + (map.get("briberyRemark")==null?"" :map.get("briberyRemark") ) , ItextFont.fangSongNormalPdf12));
        cell[8].setColspan(4);

        cell[9] = new PdfPCell(new Phrase("评审人员是否有向申请单位提出解决个人或单位利益问题的情况，如有，请详细描述：\n"+map.get("privateThing")+ (map.get("privateThingRemark")==null?"" :map.get("privateThingRemark") )      , ItextFont.fangSongNormalPdf12));
        cell[9].setColspan(4);

        cell[10] = new PdfPCell(new Phrase("评审人员是否有人参加过有偿咨询活动，如有，请详细描述：\n"+map.get("consultation")+ (map.get("consultationRemark")==null?"" :map.get("consultationRemark") )       , ItextFont.fangSongNormalPdf12));
        cell[10].setColspan(4);

        cell[11] = new PdfPCell(new Phrase("评审人员在工作作风和秉公审查方面存在的问题：\n"+ map.get("style")+ (map.get("styleRemark")==null?"" :map.get("styleRemark") )    , ItextFont.fangSongNormalPdf12));
        cell[11].setColspan(4);

        cell[12] = new PdfPCell(new Phrase("其他建议或反馈：\n"+(map.get("remark")==null?"" :map.get("remark") ), ItextFont.fangSongNormalPdf12));
        cell[12].setColspan(4);

        cell[13] = new PdfPCell(new Phrase("申请单位负责人签名、单位盖章：\n\n", ItextFont.fangSongNormalPdf12));
        cell[13].setColspan(4);


        for (int i = 0; i < 14; i++) {
            if (i >= 8) {
                cell[i].setHorizontalAlignment(Element.ALIGN_LEFT);
                cell[i].setVerticalAlignment(Element.ALIGN_TOP);
                cell[i].setMinimumHeight(70);
            } else {
                cell[i].setHorizontalAlignment(Element.ALIGN_CENTER);//居中显示
                cell[i].setVerticalAlignment(Element.ALIGN_MIDDLE);//纵向居中显示
            }
            cell[i].setPaddingTop(10.5F);
            cell[i].setPaddingBottom(10.5F);
            table.addCell(cell[i]);
        }

        Paragraph paragraph = new Paragraph();
        paragraph.add(table);
        paragraph.setSpacingBefore(5f);
        document.add(paragraph);

        paragraph = new Paragraph("备注：1、填写说明：如没有相关问题，请填写“无”；如“有”，请写明相关情况。填写完毕请单位负责人签名，加盖单位公章。"+
        "2、该表原件装入中心提供的专用信封，直接投入业务受理大厅的信访信箱或寄回上海市质量技术监督局业务受理中心。"+
        "3、该表复印件在实地审查完成后，传真于：64854610。", ItextFont.fangSongNormalPdf12);

        document.add(paragraph);
        document.close();

    }

    public static void main(String[] args) throws Exception {

        String file = "d:/qw.pdf";
        OutputStream outputStream=new FileOutputStream(file);
        Map<String,String> map=new HashMap<String,String>();
        map.put("companyName","companyName");
        map.put("checkTime","2017-10-11");
        map.put("leader","leader");
        map.put("crew","crew");
        map.put("bribery","有。");
        map.put("briberyRemark","briberyRemark");
        map.put("privateThing","有。");
        map.put("privateThingRemark","privateThingRemark");
        map.put("consultation","有。");
        map.put("consultationRemark","consultationRemark");
        map.put("style","有。");
        map.put("styleRemark","styleRemark");
        map.put("remark","remark");
        try {
            createPdf(outputStream, map);
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}


